﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Domain.Table;
using ComicQ.Controller;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for MemberEditorView.xaml
    /// </summary>
    public partial class MemberEditorView
    {
        private readonly MemberController _memberControl;
        private readonly Member _member;

        public MemberEditorView()
        {
            InitializeComponent();
            _memberControl = new MemberController();
            Title = "Add Member";
        }

        public MemberEditorView(Member member)
        {
            InitializeComponent();
            _memberControl = new MemberController();
            Title = "Edit Member";

            _member = member;

            textBoxId.Text = member.Id.ToString();
            textBoxUsername.Text = member.Username;
            textBoxPassword.Text = member.Password;
            textBoxName.Text = member.Name;
            textBoxAddress.Text = member.Address;
            textBoxPhone.Text = member.Phone;
            textBoxEmail.Text = member.Email;

            textBoxUsername.IsEnabled = false;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxUsername.Text = "";
            textBoxPassword.Text = "";
            textBoxName.Text = "";
            textBoxAddress.Text = "";
            textBoxPhone.Text = "";
            textBoxEmail.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxName.Text.Equals("") || textBoxAddress.Text.Equals("") ||
                textBoxPhone.Text.Equals("") || textBoxEmail.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                try
                {
                    var phoneDigits = textBoxPhone.Text.ToCharArray();

                    if (phoneDigits.Any(cell => !char.IsDigit(cell)))
                    {
                        throw new Exception();
                    }

                    if (Title.Equals("Add Member"))
                    {
                        var member = new Member
                                            {
                                                Username = textBoxUsername.Text,
                                                Password = textBoxPassword.Text,
                                                Name = textBoxName.Text,
                                                Address = textBoxAddress.Text,
                                                Phone = textBoxPhone.Text,
                                                Email = textBoxEmail.Text,
                                                JoinDate = DateTime.Now
                                            };

                        var message = _memberControl.AddMember(member);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses menambah member");
                                ClearTextBox();
                                break;
                            case -1:
                                MessageBox.Show("email sudah dipakai");
                                break;
                            default:
                                MessageBox.Show("username sudah ada");
                                break;
                        }
                    }
                    else
                    {
                        var newMember = new Member
                                            {
                                                Id = _member.Id,
                                                Username = textBoxUsername.Text,
                                                Password = textBoxPassword.Text,
                                                Name = textBoxName.Text,
                                                Address = textBoxAddress.Text,
                                                Email = textBoxEmail.Text,
                                                Phone = textBoxPhone.Text,
                                                JoinDate = _member.JoinDate
                                            };

                        var message = _memberControl.EditMember(newMember, _member.Email);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses mengedit member");
                                break;
                            default:
                                MessageBox.Show("email sudah dipakai");
                                break;
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("field phone harus angka");
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
